// signal_generatorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "signal_generator.h"
#include "signal_generatorDlg.h"
#include "segment.h"
#include "cmw500.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSignal_generatorDlg dialog

CSignal_generatorDlg::CSignal_generatorDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSignal_generatorDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSignal_generatorDlg)
	m_str_sig_list = _T("");
	m_logshow = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSignal_generatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSignal_generatorDlg)
	DDX_Text(pDX, IDC_EDIT_SIGNAL_LIST, m_str_sig_list);
	DDX_Text(pDX, IDC_STATIC_LOG, m_logshow);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSignal_generatorDlg, CDialog)
	//{{AFX_MSG_MAP(CSignal_generatorDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_DESTROY()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_CHANGE(IDC_EDIT_SIGNAL_LIST, OnChangeEditSignalList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSignal_generatorDlg message handlers

BOOL CSignal_generatorDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	c500 = new cmw500; 
    this->convert_string2segment_verify(); 
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSignal_generatorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

void CSignal_generatorDlg::OnDestroy()
{
	WinHelp(0L, HELP_QUIT);
	CDialog::OnDestroy();
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSignal_generatorDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSignal_generatorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSignal_generatorDlg::OnChangeEditSignalList() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
    /***
    1 2 3 4 5
    11 22 33 55 66
    33 44 55 22 11

    ***/

	CPtrArray sig; 

	// TODO: Add your control notification handler code here
    // get data from control
    this->UpdateData();

    // parsing the input string
    CString input = m_str_sig_list+"\n"; 
    CString line; 
    //MessageBox(input.GetBuffer(input.GetLength()));

    while(!input.IsEmpty()&&(input.FindOneOf("\n") >0 ))
    {
        line = input.Left(input.FindOneOf("\n")); 
        segment *t=convert_string2segment(line);
        if(NULL!=t)
        {
            sig.Add((void*) t);
        }

        //MessageBox(line); 
        input = input.Right(input.GetLength() - 1 - input.FindOneOf("\n")); //1 stand for '\n'
    }

    // make signal list
    this->c500->build_signal_array(&sig); 

    this->m_logshow += this->get_time_stamp_str() + "new signal list edited\n\r"; 
    this->UpdateData(FALSE); 
}

CString CSignal_generatorDlg::get_time_stamp_str()
{
    CTime t = CTime::GetCurrentTime(); 
    CString s = t.Format("%Y-%m-%d-%H:%M:%S:"); 
    return s; 
}

segment* CSignal_generatorDlg::convert_string2segment(CString sin)
{
    CString p=CString::CString(sin); 
    LPCTSTR lpszCharSet = " ,.;\t";
	segment *s = NULL; //new segment(); 

    int         c1 = 0;
    double      c2 = 9.0e8;
    double      c3 = -130.0;
    double      c4 = 4.61538;
    int         c5 = -1;
    int         c6 = 0; 

    // check this is within a line
    if(sin.FindOneOf("\n") == -1)
    {
         //while 
    }
    return s;
}

bool CSignal_generatorDlg::convert_string2segment_verify()
{
    CString v2 = CString("1234");
    int x = atoi((char*)LPCTSTR(v2)); 

    CString v1 = CString("101, 103, 104, 105, 10000"); 
    this->convert_string2segment(v1);
    return 0;
}
